import Singleton from "../net/Singleton";

export default class CsvUtils extends Singleton {

    private getValueByType(type, data, name, k, i) {
        switch (type) {
            case "number":
                return Number(data)
            case "float":
                return Number(data)
            case "string":
                return data == "-1" ? null : data
            case "idCount":
                if (data != '-1' && data != '') {
                    let _idCount = data.split(':');
                    if (_idCount.length == 2) {
                        return {
                            id: Number(_idCount[0]),
                            count: Number(_idCount[1])
                        }
                    } else {
                        cc.warn(' error idCount:', data, name, k, i)
                        return null
                    }
                } else {
                    return null
                }
            case "listNumString":
                if (data != '-1' && data != '') {
                    let _list = data.split('|');
                    let _listdata = []
                    for (let x = 0; x < _list.length; x++) {
                        let _idCount = _list[x].split(':');
                        if (_idCount.length == 2) {
                            _listdata.push({
                                num: Number(_idCount[0]),
                                string: _idCount[1] == "-1" ? null : _idCount[1]
                            })
                        } else {
                            cc.warn(' error listNumString:', data, name, k, i)
                            return null
                        }
                    }
                    return _listdata
                } else {
                    return null
                }
            case "numString":
                if (data != '-1' && data != '') {
                    let _idCount = data.split(':');
                    if (_idCount.length == 2) {
                        return {
                            num: Number(_idCount[0]),
                            string: _idCount[1] == "-1" ? null : _idCount[1]
                        }
                    } else {
                        cc.warn(' error numString:', data, name, k, i)
                        return null
                    }
                } else {
                    return null
                }
            case "listString":
                if (data != '-1' && data != '') {
                    return data.split('|');
                } else {
                    return null
                }
            case "listIdNumber":
                if (data != '-1' && data != '') {
                    let _list = data.split('|');
                    let _listdata = []
                    for (let x = 0; x < _list.length; x++) {
                        let _idCount = _list[x].split(':');
                        if (_idCount.length == 2) {
                            _listdata.push({
                                id: Number(_idCount[0]),
                                num: Number(_idCount[1])
                            })
                        } else {
                            cc.warn(' error listIdNumber:', data, name, k, i)
                            return null
                        }
                    }
                    return _listdata
                } else {
                    return null
                }
            case "listIdCount":
                if (data != '-1' && data != '') {
                    let _list = data.split('|');
                    let _listdata = []
                    for (let x = 0; x < _list.length; x++) {
                        let _idCount = _list[x].split(':');
                        if (_idCount.length == 2) {
                            _listdata.push({
                                id: Number(_idCount[0]),
                                count: Number(_idCount[1])
                            })
                        } else {
                            cc.warn(' error listIdCount:', data, name, k, i)
                            return null
                        }
                    }
                    return _listdata
                } else {
                    return null
                }
            case "listNumber":
                if (data != '-1' && data != '') {
                    let _list = data.split('|');
                    let _listdata = []
                    for (let x = 0; x < _list.length; x++) {
                        _listdata.push(Number(_list[x]))
                    }
                    return _listdata
                } else {
                    return null
                }
            case "listArray":
                if (data != '-1' && data != '') {
                    let _list = data.split(';');
                    let _listdata = []
                    for (let x = 0; x < _list.length; x++) {
                        let _arr = _list[x].split('|');
                        let _numbers = []
                        for (let y = 0; y < _arr.length; y++) {
                            _numbers.push(Number(_arr[y]))
                        }
                        _listdata.push(_numbers)
                    }
                    return _listdata
                } else {
                    return null
                }
            default:
                cc.warn('类型错误', type, name, k, i)
                return null
        }
    }

    private csvHead = {}
    private jsonTo(json, name) {
        if (this.csvHead[name]) {
            json = JSON.parse(json)
            let data = {}
            for (let i = 0; i < json.length; i++) {
                let one = json[i]
                let _t = {}
                for (let k in one) {
                    if (this.csvHead[name].head[k]) {
                        _t[k] = this.getValueByType(this.csvHead[name].head[k], one[k], name, k, i)
                    }
                }
                let arr = data
                let max = this.csvHead[name].mainKeys.length
                for (let j = 0; j < max; j++) {
                    let field = this.csvHead[name].mainKeys[j]
                    let vv = _t[field]
                    if (j < (max - 1)) {
                        if (!arr[vv]) {
                            arr[vv] = {}
                        }
                        arr = arr[vv]
                    } else {
                        arr[vv] = _t
                    }
                }
            }
            return data
        }
        return null
    }
    // csv转json
    public csv2json(res, keys, name) {
        let json = {};
        let row = 0;//行
        let column = 0;//列
        let typeJson = [];
        let keyJson = [];
        let lineStr: string = "";
        let lineStrs: string[] = [];
        let lineJson = null;
        let i = 0;
        let j = 0;
        let strs = res.split('\r\n');
        if (strs.length < 2) {
            strs = res.split('\n');
        }
        row = strs.length;
        //保存配置解析方式
        this.csvHead[name] = {
            mainKeys: keys,
            head: {}
        }
        //跳过第一行说明
        //类型
        for (i = 1; i < 2; ++i) {
            lineStr = strs[i];
            lineStrs = lineStr.split(',');
            column = lineStrs.length;
            lineJson = [];
            for (j = 0; j < column; j++) {
                lineJson[j] = (lineStrs[j]);
            }
            typeJson = lineJson;
        }
        //字段名
        for (i = 2; i < 3; ++i) {
            lineStr = strs[i];
            lineStrs = lineStr.split(',');
            column = lineStrs.length;
            lineJson = [];
            for (j = 0; j < column; j++) {
                lineJson[j] = (lineStrs[j]);
            }
            keyJson = lineJson;
        }
        for (i = 0; i < keyJson.length; ++i) {
            this.csvHead[name].head[keyJson[i]] = typeJson[i]
        }
        //数据
        for (i = 3; i < row; ++i) {
            lineStr = strs[i];
            if (lineStr.length == 0) {
                break;
            }
            lineStrs = lineStr.split(',');
            column = lineStrs.length;
            lineJson = {};
            for (j = 0; j < column; j++) {
                lineJson[keyJson[j]] = this.getValueByType(typeJson[j], lineStrs[j], name, keyJson[j], i);
            }
            let arr = json
            let max = keys.length
            for (let i = 0; i < max; i++) {
                if (i < (max - 1)) {
                    if (!arr[lineJson[keys[i]]]) {
                        arr[lineJson[keys[i]]] = {}
                    }
                    arr = arr[lineJson[keys[i]]]
                } else {
                    arr[lineJson[keys[i]]] = lineJson
                }
            }
        }
        return json;
    }


}